﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Mono.Data.Sqlite;
using UnityEngine;
using UnityEngine.UI;

public class LoadDataPanelView : MonoBehaviour
{
    //存档路径
    private string _saveDataPath;

    private string[] _saveDatas;

    private GameObject[] btns=new GameObject[3];
    
    void Start()
    {
        _saveDataPath = Application.dataPath + "/../Save/";
        if (!Directory.Exists(_saveDataPath))
        {
            Directory.CreateDirectory(_saveDataPath);
        }
        _saveDatas=new []{"SaveData1.db","SaveData2.db","SaveData3.db"} ;

        for (int i = 0; i < transform.childCount; i++)
        {
            btns[i] = transform.GetChild(i).gameObject;
        }

        InitUI();
    }

    private void InitUI()
    {
        var info = GetAllSaveDataInfo();
        for (int i = 0; i < info.Count; i++)
        {
            if (info[i])
            {
                //加载数据 跳转菜单界面
                btns[i].GetComponentInChildren<Text>().text = $"存档{i+1}";   
                btns[i].GetComponent<Button>().onClick.RemoveAllListeners();
                int index = i;
                btns[i].GetComponent<Button>().onClick.AddListener(() =>
                {
                    Debug.Log("点击跳转页面");
                    //加载数据
                    StartGame.Instance._sqlDbConn = new SqlDbConnect(_saveDataPath + _saveDatas[index]);
                     CharacterModel.Instance.LoadData(StartGame.Instance._sqlDbConn);
                     Instantiate(PrefabManager.Instance.MenuPanel,StartGame.Instance.canvas).AddComponent<MenuPanelView>();
                     Debug.LogWarning(StartGame.Instance._sqlDbConn);
                     Destroy(gameObject);
                });
                
            }
            else
            {
                //创建存档
                int temp = i;
                btns[i].GetComponent<Button>().onClick.RemoveAllListeners();
                btns[i].GetComponent<Button>().onClick.AddListener(() =>
                {
                    Debug.Log($"创建存档{temp+1}");
                    CreateSaveData(temp);
                    // btns[temp].GetComponentInChildren<Text>().text = $"存档{temp+1}";    
                    InitUI();
                });
                
            }
        }
    }
    

    #region Util

    private Dictionary<int, bool> GetAllSaveDataInfo()
    {
        Dictionary<int, bool> ret = new Dictionary<int, bool>();
        for (int i = 0; i < _saveDatas.Length; i++)
        {
            //如果这个存档存在
            if (File.Exists(_saveDataPath + _saveDatas[i]))
            {
              ret.Add(i,true);
            }
            else
            {
                ret.Add(i,false);

            }
        }
        return ret;
    }

    /// <summary>
    /// 创建数据库文件
    /// </summary>
    /// <param name="index"></param>
    private void CreateSaveData(int index)
    {
        
        if(SqlDbConnect.CreateDbSqlite(_saveDataPath + _saveDatas[index]))
           InitDbData(index);
    }

    ///创建默认的表和表中信息
    private void InitDbData(int index)
    {
        StartGame.Instance._sqlDbConn = new SqlDbConnect(_saveDataPath + _saveDatas[index]);
        List<CharacterSqliteData> CharacterDatas = new List<CharacterSqliteData>()
        {
            new CharacterSqliteData()
            {
                Id = 1,
                Age = 14,
                Height = 1.46f,
                Name ="小蓝",
                Sex = false
            },
            new CharacterSqliteData()
            {
                Id = 2,
                Age = 15,
                Height = 1.6f,
                Name ="小红",
                Sex = true
            },
        };
        List<ItemData> ItemDatas = new List<ItemData>()
        {
            new ItemData()
            {
                Id=1,
                ItemId = 1,
                ItemName = "雨伞",
                CharacterId = 1,
                ItemCount = 2
            },
            new ItemData()
            {
                Id=2,
                ItemId = 1,
                ItemName = "雨伞",
                CharacterId = 2,
                ItemCount = 1
            },
            new ItemData()
            {
                Id=3,
                ItemId = 2,
                ItemName = "糖果",
                CharacterId = 2,
                ItemCount = 1
            },
            new ItemData()
            {
                Id=4,
                ItemId = 3,
                ItemName = "橘子",
                CharacterId = 1,
                ItemCount = 3
            },
            new ItemData()
            {
                Id=5,
                ItemId = 4,
                ItemName = "苹果",
                CharacterId = 2,
                ItemCount = 5
            }
        };
        if (StartGame.Instance._sqlDbConn.CreateTable<CharacterSqliteData>()!=-1)
        {
            StartGame.Instance._sqlDbConn.Insert(CharacterDatas);
        }
        // if (!_sqlite.IsTableCreated<ItemData>())
        // {
        //     _sqlite.CreateTable<ItemData>();
        // }
        if (StartGame.Instance._sqlDbConn.CreateTable<ItemData>() != -1)
        {
            StartGame.Instance._sqlDbConn.Insert(ItemDatas);
        }
    }
    #endregion
}
